<?php
/**
 * +--------------------------------------------------------------
 * | eLuoPHP
 * +--------------------------------------------------------------
 * | Copyright (c) 2016-2018 http://qxwoo.cn All rights reserved.
 * +--------------------------------------------------------------
 * | y139w [y139w@163.com]
 * +--------------------------------------------------------------
 * | 响应处理类文件
 * +--------------------------------------------------------------
 */

namespace luo;

use luo\response\Json as JsonResponse;

class Response{

    /**
     * 数据
     * @var string
     */
    protected $data;

    /**
     * 转换后的数据
     * @var string
     */
    protected $content = null;

    /**
     * 类型
     * @var string
     */
    protected $contentType = 'text/html';

    /**
     * 编码格式
     * @var string
     */
    protected $charset = 'utf-8';

    /**
     * 响应头
     * @var array
     */
    protected $header = [];


    /**
     * 状态码
     * @var int
     */
    protected $code = 200;

    /**
     * Response constructor.
     * @param string $data
     * @param int $code
     * @param array $header
     */
    public function __construct($data = '', $code = 200, array $header = []){

        $this->data   = $data;
        $this->header = $header;
        $this->code   = $code;
        $this->header['Content-Type'] = $this->contentType . '; charset=' . $this->charset;

    }

    /**
     * 创建响应
     * @param string $data
     * @param string $type
     * @param int $code
     * @param array $header
     * @return static
     */
    public static function create($data = '', $type = '', $code = 200, array $header = []){

        $type = empty($type) ? 'null' : strtolower($type);

        $class = ( false !== strpos($type, '\\') ) ? $type : '\\to\\response\\' . ucfirst($type);
        if (class_exists($class)) {
            $response = new $class($data, $code, $header);
        } else {
            $response = new static($data, $code, $header);
        }

        return $response;

    }

    /**
     * 输出内容
     */
    public function send(){

        $data = $this->content($this->data);

        if (!headers_sent($file) && !empty($this->header)) {
            // 发送状态码
            http_response_code($this->code);
            // 发送头部信息
            foreach ($this->header as $name => $val) {
                header($name . ':' . $val);
            }
        }

        // 输出内容
        echo $data;

        if (function_exists('fastcgi_finish_request')) {
            // 提高页面响应
            fastcgi_finish_request();
        }

    }

    /**
     * 处理响应数据
     * @param $data
     * @return mixed
     */
    protected function output($data){

        return $data;

    }

    /**
     * 处理响应数据
     * @param $data
     * @return mixed|string
     */
    public function content($data){

        $content = $this->output($data);

        $this->content = $content;

        return $this->content;

    }

}

